package com.namnguyen.gamepuzzle2048.activity;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.namnguyen.gamepuzzle2048.R;
import com.namnguyen.gamepuzzle2048.listener.OnSwipeTouchListener;
import com.namnguyen.gamepuzzle2048.model.Brick;

public class MainActivity extends Activity {

	//	1	2	3	4
	//	5	6	7	8
	//	9	10	11	12
	//	13	14	15	16	
	
	Brick[] array = new Brick[16];
	ArrayList<Brick> brickList = new ArrayList<Brick>();
	TableLayout tableLayout;
	TableRow tr1;
	TableRow tr2;
	TableRow tr3;
	TableRow tr4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initBoard();
		
		initBrick();
		
		// This code below is for testing (remove on release)
		updateImageByNumber(0, 4);
		updateImageByNumber(2, 4);
//		updateImageByNumber(2, 4);
//		updateImageByNumber(3, 8);
	}
	
	private void initBoard() {
		
		tableLayout = (TableLayout) findViewById(R.id.TableLayout1);
		
		tableLayout.setOnTouchListener(new OnSwipeTouchListener(this.getBaseContext()) {
		    @Override
		    public void onSwipeLeft() {
		    	left();
		    }
		    
		    public void onSwipeRight() {
		    	Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
		    }
		    
		    public void onSwipeTop() {
		    	Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
		    }
		    
		    public void onSwipeBottom() {
		    	Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
		    }
		});
		
		tr1 = new TableRow(this);
        tr1.setLayoutParams(new TableRow.LayoutParams(
        		TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f));
        
        tr2 = new TableRow(this);
        tr2.setLayoutParams(new TableRow.LayoutParams(
        		TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f));
        
        tr3 = new TableRow(this);
        tr3.setLayoutParams(new TableRow.LayoutParams(
        		TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f));
        
        tr4 = new TableRow(this);
        tr4.setLayoutParams(new TableRow.LayoutParams(
        		TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1f));
        
	}
	
	private void initBrick() {
		
		for(int i = 0 ; i < 16; i++) {
			Brick brick = new Brick(this.getBaseContext(), 0, i + 1);
			brick.setNumber(0);
			brick.setImageResource(R.drawable.brick_0);
			array[i] = brick;
		}
		
		for (int i = 0 ; i < 16; i++) {
        	if (i < 4) {
        		tr1.addView(array[i]);
        	} else if (i < 8) {
        		tr2.addView(array[i]);
        	} else if (i < 12) {
        		tr3.addView(array[i]);
        	} else {
        		tr4.addView(array[i]);
        	}
        }
        
        tableLayout.addView(tr1, new TableLayout.LayoutParams(
        		TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        tableLayout.addView(tr2, new TableLayout.LayoutParams(
        		TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        tableLayout.addView(tr3, new TableLayout.LayoutParams(
        		TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        tableLayout.addView(tr4, new TableLayout.LayoutParams(
        		TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        
        // randomly generate 2 brick
        // rem for debug
//        for (int i = 0; i < 2; i++) {
//        	generateBrick();
//        }
	}
	
	private void updateImageByNumber(int pos, int number) {

		array[pos].setNumber(number);
		
		Uri uri = Uri.parse("android.resource://com.namnguyen.gamepuzzle2048/drawable/brick_"+number);
		
		array[pos].setImageURI(uri);
	}
	
	private void generateBrick() {
		
		int chance2 = 70;
		
		int pos = new Random().nextInt(15) + 1;
		
		while (array[pos].getNumber() != 0) {
			pos = new Random().nextInt(15) + 1;
		}
		
		int d = new Random().nextInt() * 100;
		if (d < chance2) {
			updateImageByNumber(pos, 2);
		} else {
			updateImageByNumber(pos, 4);
		}
	}

	private void left() {
		// check if movable
		if (isMovableLeft()) {
			
			// For Row 1
			if (array[0].getNumber() == 0 
					&& array[0].getNumber() == array[1].getNumber() 
					&& array[0].getNumber() == array[2].getNumber() 
					&& array[0].getNumber() == array[3].getNumber() 
					|| array[0].getNumber() != array[1].getNumber()
					&& array[1].getNumber() != array[2].getNumber()
					&& array[2].getNumber() != array[3].getNumber()) {
				// Do nothing
			}
			
			if (array[0].getNumber() == 0
					&& array[1].getNumber() == 0
					&& array[2].getNumber() == 0
					&& array[3].getNumber() != 0) {
				updateImageByNumber(0, array[3].getNumber());
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() == 0
					&& array[1].getNumber() == 0
					&& array[2].getNumber() != 0
					&& array[3].getNumber() == 0) {
				updateImageByNumber(0, array[2].getNumber());
				updateImageByNumber(2, 0);
			} else if (array[0].getNumber() == 0
					&& array[1].getNumber() != 0
					&& array[2].getNumber() == 0
					&& array[3].getNumber() == 0) {
				updateImageByNumber(0, array[1].getNumber());
				updateImageByNumber(1, 0);
			} else if (array[0].getNumber() != 0
					&& array[1].getNumber() == 0
					&& array[2].getNumber() == 0
					&& array[3].getNumber() == 0) {
				// Do nothing
			} else if (array[0].getNumber() == 0
					&& array[1].getNumber() == 0
					&& array[2].getNumber() != 0
					&& array[2].getNumber() == array[3].getNumber() ) {
				updateImageByNumber(0, array[2].getNumber()*2);
				updateImageByNumber(2, 0);
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() == 0
					&& array[1].getNumber() != 0
					&& array[1].getNumber() != array[2].getNumber()
					&& array[2].getNumber() == array[3].getNumber()) {
				updateImageByNumber(0, array[1].getNumber());
				updateImageByNumber(1, array[2].getNumber()*2);
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() == 0
					&& array[1].getNumber() != 0
					&& array[1].getNumber() != array[2].getNumber()
					&& array[2].getNumber() != array[3].getNumber()) {
				updateImageByNumber(0, array[1].getNumber());
				updateImageByNumber(1, array[2].getNumber());
				updateImageByNumber(2, array[3].getNumber());
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() != 0
					&& array[0].getNumber() == array[1].getNumber()
					&& array[2].getNumber() == array[3].getNumber()) {
				updateImageByNumber(0, array[1].getNumber()*2);
				updateImageByNumber(1, array[2].getNumber()*2);
				updateImageByNumber(2, 0);
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() != 0						// <== added from here
					&& array[0].getNumber() == array[3].getNumber()
					&& array[1].getNumber() == 0
					&& array[2].getNumber() == 0) {
				updateImageByNumber(0, array[0].getNumber()*2);
				updateImageByNumber(3, 0);
			} else if (array[0].getNumber() != 0						
					&& array[0].getNumber() == array[2].getNumber()
					&& array[1].getNumber() == 0
					&& array[3].getNumber() == 0) {
				updateImageByNumber(0, array[0].getNumber()*2);
				updateImageByNumber(2, 0);
			}
			
			// For Row 2
			if (array[4].getNumber() == 0 
					&& array[4].getNumber() == array[5].getNumber() 
					&& array[4].getNumber() == array[6].getNumber() 
					&& array[4].getNumber() == array[7].getNumber() 
					|| array[4].getNumber() != array[5].getNumber()
					&& array[5].getNumber() != array[6].getNumber()
					&& array[6].getNumber() != array[7].getNumber()) {
				// Do nothing
			}
			
			if (array[4].getNumber() == 0
					&& array[5].getNumber() == 0
					&& array[6].getNumber() == 0
					&& array[7].getNumber() != 0) {
				updateImageByNumber(4, array[7].getNumber());
				updateImageByNumber(7, 0);
			} else if (array[4].getNumber() == 0
					&& array[5].getNumber() == 0
					&& array[6].getNumber() != 0
					&& array[7].getNumber() == 0) {
				updateImageByNumber(4, array[6].getNumber());
				updateImageByNumber(6, 0);
			} else if (array[4].getNumber() == 0
					&& array[5].getNumber() != 0
					&& array[6].getNumber() == 0
					&& array[7].getNumber() == 0) {
				updateImageByNumber(4, array[5].getNumber());
				updateImageByNumber(5, 0);
			} else if (array[4].getNumber() != 0
					&& array[5].getNumber() == 0
					&& array[6].getNumber() == 0
					&& array[7].getNumber() == 0) {
				// Do nothing
			} else if (array[4].getNumber() == 0
					&& array[5].getNumber() == 0
					&& array[6].getNumber() != 0
					&& array[6].getNumber() == array[7].getNumber() ) {
				updateImageByNumber(4, array[6].getNumber()*2);
				updateImageByNumber(6, 0);
				updateImageByNumber(7, 0);
			} else if (array[4].getNumber() == 0
					&& array[5].getNumber() != 0
					&& array[5].getNumber() != array[6].getNumber()
					&& array[6].getNumber() == array[7].getNumber()) {
				updateImageByNumber(4, array[5].getNumber());
				updateImageByNumber(5, array[6].getNumber()*2);
				updateImageByNumber(7, 0);
			} else if (array[4].getNumber() == 0
					&& array[5].getNumber() != 0
					&& array[5].getNumber() != array[6].getNumber()
					&& array[6].getNumber() != array[7].getNumber()) {
				updateImageByNumber(4, array[5].getNumber());
				updateImageByNumber(5, array[6].getNumber());
				updateImageByNumber(6, array[7].getNumber());
				updateImageByNumber(7, 0);
			} else if (array[4].getNumber() != 0
					&& array[4].getNumber() == array[5].getNumber()
					&& array[6].getNumber() == array[7].getNumber()) {
				updateImageByNumber(4, array[5].getNumber()*2);
				updateImageByNumber(5, array[6].getNumber()*2);
				updateImageByNumber(6, 0);
				updateImageByNumber(7, 0);
			}
			
			// For Row 3
			if (array[8].getNumber() == 0 
					&& array[8].getNumber() == array[9].getNumber() 
					&& array[8].getNumber() == array[10].getNumber() 
					&& array[8].getNumber() == array[11].getNumber() 
					|| array[8].getNumber() != array[9].getNumber()
					&& array[9].getNumber() != array[10].getNumber()
					&& array[10].getNumber() != array[11].getNumber()) {
				// Do nothing
			}
			
			if (array[8].getNumber() == 0
					&& array[9].getNumber() == 0
					&& array[10].getNumber() == 0
					&& array[11].getNumber() != 0) {
				updateImageByNumber(8, array[11].getNumber());
				updateImageByNumber(11, 0);
			} else if (array[8].getNumber() == 0
					&& array[9].getNumber() == 0
					&& array[10].getNumber() != 0
					&& array[11].getNumber() == 0) {
				updateImageByNumber(8, array[10].getNumber());
				updateImageByNumber(10, 0);
			} else if (array[8].getNumber() == 0
					&& array[9].getNumber() != 0
					&& array[10].getNumber() == 0
					&& array[11].getNumber() == 0) {
				updateImageByNumber(8, array[9].getNumber());
				updateImageByNumber(9, 0);
			} else if (array[8].getNumber() != 0
					&& array[9].getNumber() == 0
					&& array[10].getNumber() == 0
					&& array[11].getNumber() == 0) {
				// Do nothing
			} else if (array[8].getNumber() == 0
					&& array[9].getNumber() == 0
					&& array[10].getNumber() != 0
					&& array[10].getNumber() == array[11].getNumber() ) {
				updateImageByNumber(8, array[10].getNumber()*2);
				updateImageByNumber(10, 0);
				updateImageByNumber(11, 0);
			} else if (array[8].getNumber() == 0
					&& array[9].getNumber() != 0
					&& array[9].getNumber() != array[10].getNumber()
					&& array[10].getNumber() == array[11].getNumber()) {
				updateImageByNumber(8, array[9].getNumber());
				updateImageByNumber(9, array[10].getNumber()*2);
				updateImageByNumber(11, 0);
			} else if (array[8].getNumber() == 0
					&& array[9].getNumber() != 0
					&& array[9].getNumber() != array[10].getNumber()
					&& array[10].getNumber() != array[11].getNumber()) {
				updateImageByNumber(8, array[9].getNumber());
				updateImageByNumber(9, array[10].getNumber());
				updateImageByNumber(10, array[11].getNumber());
				updateImageByNumber(11, 0);
			} else if (array[8].getNumber() != 0
					&& array[8].getNumber() == array[9].getNumber()
					&& array[10].getNumber() == array[11].getNumber()) {
				updateImageByNumber(8, array[9].getNumber()*2);
				updateImageByNumber(9, array[10].getNumber()*2);
				updateImageByNumber(10, 0);
				updateImageByNumber(11, 0);
			}
			
			// For Row 4
			if (array[12].getNumber() == 0 
					&& array[12].getNumber() == array[13].getNumber() 
					&& array[12].getNumber() == array[14].getNumber() 
					&& array[12].getNumber() == array[15].getNumber() 
					|| array[12].getNumber() != array[13].getNumber()
					&& array[13].getNumber() != array[14].getNumber()
					&& array[14].getNumber() != array[15].getNumber()) {
				// Do nothing
			}
			
			if (array[12].getNumber() == 0
					&& array[13].getNumber() == 0
					&& array[14].getNumber() == 0
					&& array[15].getNumber() != 0) {
				updateImageByNumber(12, array[15].getNumber());
				updateImageByNumber(15, 0);
			} else if (array[12].getNumber() == 0
					&& array[13].getNumber() == 0
					&& array[14].getNumber() != 0
					&& array[15].getNumber() == 0) {
				updateImageByNumber(12, array[14].getNumber());
				updateImageByNumber(14, 0);
			} else if (array[12].getNumber() == 0
					&& array[13].getNumber() != 0
					&& array[14].getNumber() == 0
					&& array[15].getNumber() == 0) {
				updateImageByNumber(12, array[13].getNumber());
				updateImageByNumber(13, 0);
			} else if (array[12].getNumber() != 0
					&& array[13].getNumber() == 0
					&& array[14].getNumber() == 0
					&& array[15].getNumber() == 0) {
				// Do nothing
			} else if (array[12].getNumber() == 0
					&& array[13].getNumber() == 0
					&& array[14].getNumber() != 0
					&& array[14].getNumber() == array[15].getNumber() ) {
				updateImageByNumber(12, array[14].getNumber()*2);
				updateImageByNumber(14, 0);
				updateImageByNumber(15, 0);
			} else if (array[12].getNumber() == 0
					&& array[13].getNumber() != 0
					&& array[13].getNumber() != array[14].getNumber()
					&& array[14].getNumber() == array[15].getNumber()) {
				updateImageByNumber(12, array[13].getNumber());
				updateImageByNumber(13, array[14].getNumber()*2);
				updateImageByNumber(15, 0);
			} else if (array[12].getNumber() == 0
					&& array[13].getNumber() != 0
					&& array[13].getNumber() != array[14].getNumber()
					&& array[14].getNumber() != array[15].getNumber()) {
				updateImageByNumber(12, array[13].getNumber());
				updateImageByNumber(13, array[14].getNumber());
				updateImageByNumber(14, array[15].getNumber());
				updateImageByNumber(15, 0);
			} else if (array[12].getNumber() != 0
					&& array[12].getNumber() == array[13].getNumber()
					&& array[14].getNumber() == array[15].getNumber()) {
				updateImageByNumber(12, array[13].getNumber()*2);
				updateImageByNumber(13, array[14].getNumber()*2);
				updateImageByNumber(14, 0);
				updateImageByNumber(15, 0);
			}
			generateBrick();
		}
	}
	
	private boolean isMovableLeft() {
		if (array[0].getNumber() == 0 || array[4].getNumber() == 0 || 
				array[8].getNumber() == 0 || array[12].getNumber() == 0) {
			return true;
		} else if (array[0].getNumber() == array[1].getNumber() || array[1].getNumber() == array[2].getNumber() || 
				array[2].getNumber() == array[3].getNumber()) {
			return true;
		} else if (array[4].getNumber() == array[5].getNumber() || array[5].getNumber() == array[6].getNumber() || 
				array[6].getNumber() == array[7].getNumber()) {
			return true;
		} else if (array[8].getNumber() == array[9].getNumber() || array[9].getNumber() == array[10].getNumber() || 
				array[10].getNumber() == array[11].getNumber()) {
			return true;
		} else if (array[12].getNumber() == array[13].getNumber() || array[13].getNumber() == array[14].getNumber() || 
				array[14].getNumber() == array[15].getNumber()) {
			return true;
		} else {
			return false;
		}		
	}
}
